<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="155px" class='formBoxs'>
    <!-- 旧密码 -->
    <el-form-item :label="$t('userInfo.label.oldPassword')" prop="oldPassword">
      <el-input
        v-model="user.oldPassword"
        :placeholder="$t('userInfo.input.oldPassword')"
        type="password"
		show-password
		clearable/>
			
    </el-form-item>
    <!-- 新密码 -->
    <el-form-item :label="$t('userInfo.label.newPassword')" prop="newPassword">
      <el-input
        v-model="user.newPassword"
        :placeholder="$t('userInfo.input.newPassword')"
        type="password" 
		show-password
		clearable
      />
    </el-form-item>
    <!-- 确认密码 -->
    <el-form-item
      :label="$t('userInfo.label.confirmPassword')"
      prop="confirmPassword"
    >
      <el-input
        v-model="user.confirmPassword"
        :placeholder="$t('userInfo.input.confirmPassword')"
        type="password"
		show-password
		clearable
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">
        <!-- 改变密码 -->
        {{ $t('userInfo.button.confirm') }}
      </el-button>
      <el-button type="danger" size="mini" @click="close">
        <!-- 取消 -->
        {{ $t('userInfo.button.cancel') }}
      </el-button>
			<el-button @click='getPsd'>
				密码生成
			</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserPwd } from '@/api/system/user'

export default {
  data () {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error('Two inputs don\'t match!'))
      } else {
        callback()
      }
    }
    return {
      test: '1test',
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      // 表单校验
      rules: {
        oldPassword: [
          // 旧密码
          {
            required: true,
            message: this.$t('userInfo.rules.oldPassword'),
            trigger: 'blur'
          }
        ],
        newPassword: [
          // 新密码
          {
            required: true,
            message: this.$t('userInfo.rules.newPassword'),
            trigger: 'blur'
          },
		  { min: 8, max:16, message: '长度需要在8-16个字符之间',trigger: 'blur'  },
		  {
		    pattern: /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/,
		    message: '密码至少包含1个大写字母，1个小写字母,一个数字和一个特殊字符(特殊字符包括：! @ # $ % ^ & * ? )',
		    trigger: 'blur'
		  }		
          // { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          // 确认密码
          {
            required: true,
           message: this.$t('userInfo.rules.confirmPassword'),
            trigger: 'blur'
          },
          { required: true, validator: equalToPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submit () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          updateUserPwd(this.user.oldPassword, this.user.newPassword).then(
            response => {
              this.msgSuccess('修改成功')
			  this.$router.go(-1) // 击跳转至上次浏览页面
            }
          )
        }
      })
    },
    close () {
      console.log('1')
      // this.$store.dispatch("tagsView/delView", this.$route);
      // this.$router.push({ path: "/" });
      this.$router.go(-1) // 击跳转至上次浏览页面
    },
	getPsd(){
		let arr=[
			['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'],
			['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'],
			[0,1,2,3,4,5,6,7,8,9],
			['!','@','#','$','%','^','&','*','?'],
		]
		let str=''
		for(let i=0;i<8;i++){
			let index=Math.round(Math.random()*3)
			let x=Math.round(Math.random()*25);
			let y=Math.round(Math.random()*9);
			let z=Math.round(Math.random()*8);
			if(arr[index].length==26){
				str+=arr[index][x]
			}else if(arr[index].length==10){
				str+=arr[index][y]
			}else{
				str+=arr[index][z]
			}
		}
		let num=[0,0,0,0]
		let v=Math.round(Math.random()*8);
		for(let i=0;i<4;i++){
			for(let j=0;j<arr[i].length;j++){
				if(str.indexOf(arr[i][j])==-1){
					num[i]++
				}
			}
			if(arr[i].length==num[i]){
				str+=arr[i][v]
			}
		}
		this.user.newPassword=str
	}
  }
}
</script>
<style lang="scss" scoped>
	 ::v-deep.formBoxs{
		.el-form-item{
			margin-bottom:20px;
		}
	}
</style>